package com.example.appmanager.ui.gallery;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.appmanager.Appconfig.Appconfig;
import com.example.appmanager.Appconfig.GlideImageLoader;
import com.example.appmanager.MainActivity;
import com.example.appmanager.R;
import com.example.appmanager.adapter.AppAdapter;
import com.example.appmanager.entity.AppEntity;
import com.example.appmanager.entity.AppTableEntity;
import com.example.appmanager.entity.UserTableEntity;
import com.example.appmanager.services.AllresposeService;
import com.example.appmanager.user.AppTypeShow;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

/*
这个是主页Fragment
* */
public class GalleryFragment extends Fragment {
    private GalleryViewModel galleryViewModel = new GalleryViewModel();
    private TextView textView;
    private RecyclerView recyclerView;
    private ViewPager banner;
    private List<AppEntity.RowsBean> appEntities = new ArrayList<>();
    private AppAdapter appAdapter = new AppAdapter();

    private LinearLayout layoutEducation;//这个是 教育 app_education_show
    private LinearLayout layoutTrip;//这个是 旅游 app_trip_show
    private LinearLayout layoutAligence;//这个是益智 app_aligence_show
    private LinearLayout layoutMore;//这个是  app_more_show
    private LinearLayout layoutGame;//这个是  app_game_show

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        //fragment_gallery
        //nav_host_fragment
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);
        initUi(root);
        getOnClickListener(root);
        // getData();
        //galleryViewModel.setData(getContext(),userEntity,getActivity().getIntent().getStringExtra("uName"));
        galleryViewModel = ViewModelProviders.of(this).get(GalleryViewModel.class);
        /*galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<UserTableEntity>() {
            @Override
            public void onChanged(@Nullable UserTableEntity userEntity) {
                JSON json = JSONObject.parseObject(s);
                UserTableEntity userEntity = json.toJavaObject(UserTableEntity.class);
                if (!(userEntity == null)){
                    textView.setText(userEntity.getUaccount());
                }
                Log.d("GalleryFragment+1111",userEntity.toString());
            }
        });*/
        return root;
    }

    private void getOnClickListener(View root) {
        /*
        R.id.app_education_show);//这个是 教育 app_education_show
        R.id.app_trip_show);//这个是 旅游 app_trip_show
        R.id.app_aligence_show);//这个是益智 app_aligence_show
        R.id.app_more_show);//这个是  app_more_show
        R.id.app_game_show);//这个是  app_game_show
        * */
        //这里是主界面的分类跳转
            root.findViewById(R.id.app_education_show).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    jumpAppTypeShow("教育");
                }
            });
        root.findViewById(R.id.app_trip_show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpAppTypeShow("旅游");
            }
        });
        root.findViewById(R.id.app_aligence_show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpAppTypeShow("益智");
            }
        });
        root.findViewById(R.id.app_game_show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpAppTypeShow("游戏");
            }
        });
        root.findViewById(R.id.app_more_show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpAppTypeShow("");
            }
        });


    }
    private void jumpAppTypeShow(String type){
        Bundle bundle = new Bundle();
        bundle.putString("type",type);
        Intent intent = new Intent(getActivity(), AppTypeShow.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getData();
    }

    private void initUi(View view) {
        /* text_gallery */
        //textView = view.findViewById(R.id.text_gallery);
        String uname = (String) getActivity().getIntent().getExtras().get("uName");
        Log.d("GalleryFrgmentInfo的信息：+", "-->账号"+uname);
        recyclerView = view.findViewById(R.id.download_of_user);
        //banner = view.findViewById(R.id.banner);
        appAdapter = new AppAdapter(getContext(), null);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //设置图片集合
        int[] mImg = new int[]{
                R.drawable.false_back,
                R.drawable.false_back,
                R.drawable.false_back,
                R.drawable.false_back,
                R.drawable.false_back
        };

        layoutEducation = view.findViewById(R.id.app_education_show);//这个是 教育 app_education_show
        layoutTrip = view.findViewById(R.id.app_trip_show);//这个是 旅游 app_trip_show
        layoutAligence = view.findViewById(R.id.app_aligence_show);//这个是益智 app_aligence_show
        layoutMore = view.findViewById(R.id.app_more_show);//这个是  app_more_show
        layoutGame = view.findViewById(R.id.app_game_show);//这个是  app_game_show
    }
    //查看全部APP
    private void getData() {
        String uname = (String) getActivity().getIntent().getExtras().get("uName");
        Retrofit retrofit = new Retrofit.Builder().baseUrl(Appconfig.BASE_URL)
                .client(Appconfig.CLIENT) //这是设置请求的时间等信息 连接时间  上传等时间
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        AllresposeService request = retrofit.create(AllresposeService.class);
        Call<AppEntity> appCall = request.getApp(1);
        appCall.enqueue(new Callback<AppEntity>() {
            @Override
            public void onResponse(Call<AppEntity> call, Response<AppEntity> response) {
                assert response.body() != null;
                appEntities = response.body().getRows();
                Log.d("GallertFragment", appEntities.toString());
                appAdapter.setData(getContext(), appEntities);
                appAdapter.notifyDataSetChanged();

                recyclerView.setAdapter(appAdapter);
                appAdapter.setUName((String) getActivity().getIntent().getExtras().get("uName"));
                Log.d("GalleryFragment", appEntities.toString());
            }

            @Override
            public void onFailure(Call<AppEntity> call, Throwable t) {
                recyclerView.setAdapter(appAdapter);
                Log.d("Login", "请求错误" + t.getMessage());
            }
        });

    }
}
